package panels;

import game.Game;
import game.GameDriver;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Screen that will set general Game properties.
 */
public class GameConfiguration extends JPanel {

    /** Auto-generated serial version ID. */
    private static final long serialVersionUID = 3093626176648720678L;
    private Game currentGame;
    private GameDriver driver;
    private JPanel main, title, levels, map, numPlayers, buttonPanel;
    private JLabel gameConfig, mapLabel, levelLabel, numPlayerLabel;
    private JComboBox levelsList, mapsList, numPlayersList;
    private JButton next;

    private String[] levelStrings = { "Beginner", "Standard", "Tournament" };
    private String[] mapStrings = { "Standard", "Random" };
    private String[] numPlayerStrings = { "Two", "Three", "Four" };
//    private Font font;

    public GameConfiguration(Game currentGame, GameDriver driver) {

	this.currentGame = currentGame;
	this.driver = driver;
	
	gameConfig = new JLabel();
//	System.out.println(this.getClass().getResourceAsStream("/images/Herculanum.ttf").toString());
//	File font_file = new File(this.getClass().getResourceAsStream("/images/Herculanum.ttf").toString());
////	InputStream is = this.getClass().getResourceAsStream("/images/Herculanum.ttf");
//	try {
//	    font = Font.createFont(Font.TRUETYPE_FONT, font_file);
//	} catch (FontFormatException e) {
//	    // TODO Auto-generated catch block
//	    e.printStackTrace();
//	} catch (IOException e) {
//	    // TODO Auto-generated catch block
//	    e.printStackTrace();
//	}
//
//	font = font.deriveFont(12f);
//	Font font = new Font("Courier", Font.BOLD,12);
//	mapLabel.setFont(font);
//	mapLabel = new JLabel("Map");
//	
//
	mapLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/map.png")));
	mapLabel.setForeground(Color.WHITE);
	levelLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/level.png")));
	levelLabel.setForeground(Color.WHITE);

	numPlayerLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/players.png")));
	numPlayerLabel.setForeground(Color.WHITE);


	main = new JPanel();
	main.setLayout(new GridLayout(8, 1));

	title = new JPanel();
	gameConfig.setIcon(new ImageIcon(this.getClass().getResource("/images/config.png")));
	gameConfig.setBorder(null);
	title.setBorder(null);
	title.setOpaque(false);
	title.add(gameConfig);

	levels = new JPanel();
	levelsList = new JComboBox(levelStrings);
	levelsList.setSelectedIndex(0);

	levels = new JPanel();
	levels.setLayout(new GridLayout(2, 1));
	levels.add(levelLabel);
	levels.setBorder(null);
	levels.setOpaque(false);
	levels.add(levelsList);

	map = new JPanel();
	mapsList = new JComboBox(mapStrings);
	mapsList.setSelectedIndex(0);

	map = new JPanel();
	map.setLayout(new GridLayout(2, 1));
	map.setBorder(null);
	map.setOpaque(false);
	map.add(mapLabel);
	map.add(mapsList);

	numPlayers = new JPanel();
	numPlayersList = new JComboBox(numPlayerStrings);
	numPlayersList.setSelectedIndex(0);

	numPlayers = new JPanel();
	numPlayers.setLayout(new GridLayout(2, 1));
	numPlayers.add(numPlayerLabel);
	numPlayers.setBorder(null);
	numPlayers.setOpaque(false);
	numPlayers.add(numPlayersList);

	buttonPanel = new JPanel();
	next = new JButton(new ImageIcon(this.getClass().getResource("/images/continue.png")));
	next.setBorder(null);
	next.setOpaque(false);
	//next.setIcon(new ImageIcon(this.getClass().getResource("/images/start.png")));
	next.addActionListener(new ButtonListener(currentGame, driver));
	buttonPanel = new JPanel();
	buttonPanel.setOpaque(false);
	buttonPanel.add(next);

	main.add(title);
	main.add(levels);
	main.add(map);
	main.add(numPlayers);
	main.add(buttonPanel);
	main.setOpaque(false);
	add(main);
	
	setBackground(new Color(112,164,96));
	setPreferredSize(currentGame.fullPreferredSize());
    }

    public Game getCurrentGame() {
	return currentGame;
    }

    /**
     * This buttonListener will change Game properties depending on the user
     * inputed options on the screen and also will transition to the player
     * configuration screen.
     * 
     */
    private class ButtonListener implements ActionListener {

	private Game currentGame;

	public ButtonListener(Game currentGame, GameDriver driver) {
	    this.currentGame = currentGame;
	}

	public void actionPerformed(ActionEvent e) {
	    int numPlayerIndex = numPlayersList.getSelectedIndex();
	    int levelIndex = levelsList.getSelectedIndex();
	    int mapIndex = mapsList.getSelectedIndex();

	    currentGame.setPlayerArray(numPlayerIndex + 2);
	    currentGame.setDifficulty(levelIndex + 1);
	    currentGame.setMapType(mapIndex + 1);
	    currentGame.setMapTypeString(mapStrings[mapIndex]);

	    driver.playerConfig();
	}
    }
}